var rule = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/;
var separator = " -- "
var defaultTime = "0000-00-00"+separator+"0000-00-00";
var writeShow = true;
$.extend($.fn.validatebox.defaults.rules, {    
    timeRule: {
        validator: function(value,param){
        	value = $.trim(value);
        	if(value == defaultTime){
        		return true;
        	}
        	if(value.indexOf($.trim(separator))!=-1){
        		var timeArray = value.split($.trim(separator));
        		if(rule.test($.trim(timeArray[0])) && rule.test($.trim(timeArray[1]))){
        			//-- 验证时间范围 -----------------------------------
        			var timeScope = param[2];
        			if(timeScope && timeScope.length>0){
        				var min = timeScope.split(",")[0];
        				var max = timeScope.split(",")[1];
        				if(min && min.length>0 && $.trim(timeArray[0])<min){
        					$.fn.validatebox.defaults.rules.timeRule.message = '起始时间不可小于 '+min+',请重新输入';
        					return false;
        				}
        				if(max && max.length>0 && $.trim(timeArray[1])>max){
        					$.fn.validatebox.defaults.rules.timeRule.message = '终止时间不可大于'+max+'，请重新输入';
        					return false;
        				}
        			}
        			//-----------------------------------------------
        			writeShow = false;
        			$(param[0]).datebox("setValue",$.trim(timeArray[0]));
        			$(param[1]).datebox("setValue",$.trim(timeArray[1]));
        			writeShow = true;
        			return true;
        		}else{
        			$.fn.validatebox.defaults.rules.timeRule.message = '请输入正确日期格式';
        		}
        	}else{
        		$.fn.validatebox.defaults.rules.timeRule.message = '请输入正确日期格式';
        	}
        	return false;
        },
        message: '请输入正确日期格式'   
    }    
});  


$(function(){
	//去掉日期控件的日历图标，并改写字体
	$.each($("div[identifier='datePlugin'] span[class='textbox combo datebox']"),function(i,n){
		$(n).attr("style","display:none");
	});
	//对两个datebox添加 onchange事件，当值变化的时候会写到 页面上的input输入框中。
	//主要用于某些页面赋初始值的情况还有回显条件
	$.each($("div[identifier='datePlugin']"),function(i,n){
		var timeShowInput = $(n).find("input[identifier='timeShowInput']");
		var timeStart = $(n).find("input[identifier='timeStart']");
		var timeEnd = $(n).find("input[identifier='timeEnd']");
		timeStart.datebox({
			onChange:function(newValue, oldValue){
				if(writeShow){
					var timeS = timeStart.datebox("getValue");
					var timeE = timeEnd.datebox("getValue");
					timeShowInput.val(($.trim(timeS).length==0?"0000-00-00":timeS) +separator+ ($.trim(timeE).length==0?"0000-00-00":timeE));
				}
			}
		});
		timeEnd.datebox({
			onChange:function(newValue, oldValue){
				if(writeShow){
					var timeS = timeStart.datebox("getValue");
					var timeE = timeEnd.datebox("getValue");
					timeShowInput.val(($.trim(timeS).length==0?"0000-00-00":timeS) +separator+ ($.trim(timeE).length==0?"0000-00-00":timeE));
				}
			}
		});
	});
	$("button[identifier='chooseButton']").click(function(){
		selectDate($(this).parent().parent());
	});
	$("button[identifier='clear']").click(function(){
		clearDate($(this).parent().parent());
	});
	$.each($("input[identifier='timeShowInput']"),function(i,n){
		$(n).val(defaultTime);
	});
	$(".oasis-date").mask("9999-99-99 -- 9999-99-99",{placeholder:"yyyy-mm-dd -- yyyy-mm-dd"});
});

var timeParent = null;
/**
 * 选择日期，打开弹出框
 */
function selectDate(obj){
	timeParent = obj;
	var timeStart = obj.find("input[identifier='timeStart']");
	var timeEnd = obj.find("input[identifier='timeEnd']");
	var timeScope = obj.find("input[identifier='timeShowInput']").attr("timeScope");
	openSelector({
		title:'请输入您想统计或查看的时间段',
		url:BASE_PATH+"/commonController/toDatePage",
		width: 350, 
		urlparams:{timeInput_start:timeStart.datebox("getValue"),timeInput_end:timeEnd.datebox("getValue"),timeScope:timeScope},
		height:300,
		top:obj.offset().top+30,
 		left:obj.offset().left-30,
		okClose:true
});
}

/**
 * 清除日期
 */
function clearDate(obj){
	var timeStart = obj.find("input[identifier='timeStart']");
	var timeEnd = obj.find("input[identifier='timeEnd']");
	var timeShowInput = obj.find("input[identifier='timeShowInput']");
	writeShow = false;
	timeStart.datebox("setValue","");
	timeEnd.datebox("setValue","");
	timeShowInput.val(defaultTime);
	writeShow = true;
}

function openSelector(options){
	var defaults = {
		'url':'',
		'width': 800, 
		'height':500, 
		'callBack': $.noop(),
		'onLoadCallBack':$.noop(),
		'onClose':$.noop(),
		'urlparams' : {},
		'okClose':false,
		'hasConfirm':true,
		'enter':false
	};
	var settings = $.extend( {}, defaults, options );
	var title = settings['title'];
	var onCloseCallBack = settings['onClose'];
	var dg_width = settings['width'];
	var dg_height = settings['height'];
	var callBackjs = settings['callBack'];
	var urlparams = settings['urlparams'];
	var top = settings['top'];
	var left = settings['left'];
	var onLoadCallBack = settings['onLoadCallBack'];
	var enter = settings['enter'];
	var url = settings['url'];
	if(null !=$('#PageDialog') && $('#PageDialog').length>0){
		return;
	}
	var dictionaryDiv = 
	'<div id="PageDialog" class="easyui-dialog" closed="true" modal="true" >'+
		'<iframe scrolling="no" id="openListFrameId" name="openListFrameId" frameborder="0"  style="width:100%;height:inherit;"></iframe>'+
	'</div>'; 
	$(document.body).append(dictionaryDiv); 
	$('#PageDialog').dialog({
 		collapsible:false,
 		minimizable:false,
 		maximizable:false,
 		resizable:false,
 		draggable:true,
 		inline:true,
 		top:top,
 		left:left,
 		shadow:false,
 		title:title,
	    width:dg_width,
	    height:dg_height,
	    modal:true,
	    onClose:function(){
			$('#PageDialog').parent().remove();
			if(typeof onCloseCallBack == "function"){
				onCloseCallBack();
			}
	    }
	});
	var baseParams = "";
	for ( var p in urlparams) {
		baseParams = baseParams + p + "=" + urlparams[p] + "&";
	}
	if (baseParams.length > 0
			&& baseParams.charAt(baseParams.length - 1) == '&') {
		baseParams = baseParams.substring(0, baseParams.length - 1);
	}
	if(baseParams.length>0){
		url = url +"?"+baseParams;
	}
	$('#PageDialog').dialog('open');
	var listFrame = $("#openListFrameId");
	listFrame.attr("src", url); 
	listFrame.load(function() {
		if(null != onLoadCallBack){
			onLoadCallBack(openListFrameId);
		}
		if(enter){
			$("#openListFrameId").contents().find("body").bind('keyup',function(event){
				var keyValue = event.which;
				if(keyValue == KEY_VALUE.KEY_ENTER){
					var jsonData=openListFrameId.getDictionarySelections();
					if(jsonData && jsonData.length>0){
						callBackjs(jsonData);
						if(settings['okClose']){
							$('#PageDialog').dialog('close');
						}
					}
				}
			});
		}
	});
}

function writeDateBack(timeS,timeE){
	var timeStart = timeParent.find("input[identifier='timeStart']");
	var timeEnd = timeParent.find("input[identifier='timeEnd']");
	var timeShowInput = timeParent.find("input[identifier='timeShowInput']");
	writeShow = false;
	timeStart.datebox("setValue",timeS);
	timeEnd.datebox("setValue",timeE);
	timeShowInput.val(($.trim(timeS).length==0?"0000-00-00":timeS) +" -- "+ ($.trim(timeE).length==0?"0000-00-00":timeE));
	closeDialog();
	writeShow = true;
	try{
		selectDateCallBack(timeS,timeE);
	}catch(e){
		
	}
}

function closeDialog(){
	$('#PageDialog').dialog('close');
}

function selectDateCallBack(timeS,timeE){
	//TODO
}
